package application

import (
	"errors"
	"ruoyi-go-echo-ddd/internal/domain/model"
	"ruoyi-go-echo-ddd/internal/domain/service"
	"ruoyi-go-echo-ddd/internal/infrastructure/http/request"

	"github.com/labstack/echo/v4"
)

type SysUserApplication struct {
	UserService *service.SysUserService
}

func NewSysSysUserApplication(u *service.SysUserService) *SysUserApplication {
	return &SysUserApplication{
		UserService: u,
	}
}

func (s *SysUserApplication) Insert(c echo.Context, user *model.User) (*model.User, error) {
	return s.UserService.Insert(c.Request().Context(), user)
}

func (s *SysUserApplication) Update(c echo.Context, user *model.User) (int64, error) {
	return s.UserService.Update(c.Request().Context(), user)
}

func (s *SysUserApplication) SelectById(c echo.Context, id int64) (*model.User, error) {
	if id == 0 {
		return nil, errors.New("用户ID不能为空")
	}
	return s.UserService.SelectById(c.Request().Context(), id)
}
func (s *SysUserApplication) SelectByUsername(c echo.Context, username string) (*model.User, error) {
	if username == "" {
		return nil, errors.New("用户名不能为空")
	}
	return s.UserService.SelectByUsername(c.Request().Context(), username)
}

func (s *SysUserApplication) DeleteById(c echo.Context, id int64) (int64, error) {
	if id == 0 {
		return 0, errors.New("删除参数验证失败")
	}
	return s.UserService.DeleteById(c.Request().Context(), id)
}

func (s *SysUserApplication) BatchInert(c echo.Context, list []*model.User) ([]int64, error) {
	if len(list) <= 0 {
		return nil, errors.New("系统用户批量新增参数验证失败")
	}
	return s.UserService.BatchInsert(c.Request().Context(), list)
}

func (s *SysUserApplication) BatchDelete(c echo.Context, ids []any) (int64, error) {
	if len(ids) <= 0 {
		return 0, errors.New("系统用户批量删除参数验证失败")
	}
	return s.UserService.BatchDelete(c.Request().Context(), ids)
}

func (s *SysUserApplication) SelectPage(c echo.Context, param *request.UserPageParam) ([]*model.User, int64, int64, error) {
	return s.UserService.SelectPage(c.Request().Context(), param)
}

func (s *SysUserApplication) SelectList(c echo.Context, limit, offset int64) ([]*model.UserForExcel, error) {
	return s.UserService.SelectList(c.Request().Context(), limit, offset)
}

func (s *SysUserApplication) GetTotalCount(c echo.Context) (int64, error) {
	return s.UserService.GetTotalCount(c.Request().Context())
}

func (s *SysUserApplication) Login(c echo.Context, user *model.UserLogin) (*model.User, error) {
	return s.UserService.Login(c.Request().Context(), user)
}

func (s *SysUserApplication) SelectAll(c echo.Context) ([]*model.UserForExcel, error) {
	return s.UserService.SelectAll(c.Request().Context())
}
